package com.zyz.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zyz.dao.CoachDao;
import com.zyz.dao.CourseDao;
import com.zyz.dao.UserLessonLineDao;
import com.zyz.dao.UsersDao;
import com.zyz.domain.Coach;
import com.zyz.domain.Lesson;
import com.zyz.domain.User;
import com.zyz.domain.UserLessonLine;
import com.zyz.handler.UserHolder;
import com.zyz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersDao usersDao;
    @Autowired
    private CoachDao coachDao;
    @Autowired
    private UserLessonLineDao userLessonLineDao;
    @Autowired
    private CourseDao courseDao;
    //查询用户信息
    @Override
    public User findById(Integer id) {
        User user = usersDao.selectById(id);
        return user;
    }
    //更新用户
    @Override
    public void update(User user) {
        usersDao.updateById(user);
    }
    //查询用户课程id
    @Override
    public Lesson findLessinId() {
        Integer id = UserHolder.get().getId();
        QueryWrapper<UserLessonLine> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserLessonLine::getMember_id,id);
        UserLessonLine one = userLessonLineDao.selectOne(wrapper);
        Lesson lessonOne = new Lesson();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getId,id);
        User user = usersDao.selectOne(queryWrapper);
        Integer coach_id = user.getCoach_id();
        Coach coach = coachDao.selectById(coach_id);

        if (one!=null){
            Integer lesson_id = one.getLesson_id();
            QueryWrapper<Lesson> wrapperLesson = new QueryWrapper<>();
            wrapperLesson.lambda().eq(Lesson::getId,lesson_id);
            lessonOne = courseDao.selectOne(wrapperLesson);
            lessonOne.setCoach_name(coach.getCoach_name());
            lessonOne.setCoach_phone(coach.getCoach_phone());
        }
        return lessonOne;
    }
    //添加教练
    @Override
    public void addCoach(Integer id) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getId,UserHolder.get().getId());
        User user = new User();
        user.setCoach_id(id);
        usersDao.update(user,userQueryWrapper);
    }
    @Override
    public String isHave() {
        User user = UserHolder.get();
        Integer coach_id = user.getCoach_id();
        String msg=null;
        if (coach_id==null){
            msg="no";
        }else {
            msg="have";
        }
        return msg;
    }

    @Override
    public Integer getTotal() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getType,"1");
        List<User> userList = usersDao.selectList(userQueryWrapper);
        int i=0;
        for (User user : userList) {
            i++;
        }
        return i;
    }

    @Override
    public PageInfo<User> findVip(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getType,"1");
        List<User> userList = usersDao.selectList(wrapper);
        for (User user : userList) {
            Integer coach_id = user.getCoach_id();
            if (coach_id!=null) {
                QueryWrapper<Coach> coachQueryWrapper = new QueryWrapper<>();
                coachQueryWrapper.lambda().eq(Coach::getId, coach_id);
                Coach coach = coachDao.selectOne(coachQueryWrapper);

                QueryWrapper<UserLessonLine> lessonQueryWrapper = new QueryWrapper<>();
                lessonQueryWrapper.lambda().eq(UserLessonLine::getMember_id, user.getId());
                UserLessonLine lesson = userLessonLineDao.selectOne(lessonQueryWrapper);

                QueryWrapper<Lesson> lessonQueryWrapper1 = new QueryWrapper<>();
                lessonQueryWrapper1.lambda().eq(Lesson::getId, lesson.getLesson_id());
                Lesson lesson1 = courseDao.selectOne(lessonQueryWrapper1);
                user.setCoach_name(coach.getCoach_name());
                user.setLesson_name(lesson1.getLesson_name());
            }else {
                return new PageInfo<>(userList);
            }

        }
        return new PageInfo<>(userList);
    }
    //查询回显
    @Override
    public User findMessage(Integer id) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getId,id);
        User user = usersDao.selectOne(userQueryWrapper);
        return user;
    }
    //修改
    @Override
    public void updateUser(User user) {
        usersDao.updateById(user);
    }
    //删除用户
    @Override
    public void deleteUser(Integer id) {
        usersDao.deleteById(id);
    }
    //新增
    @Override
    public void insertUser(User user) {
        User user1 = new User();
        user1.setId((int) IdUtil.getSnowflake(1,1).nextId());
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setAge(user.getAge());
        user1.setPhone(user.getPhone());
        user1.setAddress(user.getAddress());
        user1.setType(1);
        usersDao.insert(user1);
    }

}
